require "rubygems"
require "active_record"

ActiveRecord::Base.establish_connection(
  :adapter => 'oracle_enhanced',
  :database => 'BMCSMDB',
  :username => 'webuser',
  :password => 'bmcdevdb',
  :host => '10.18.8.111',
  :cursor_sharing => 'similar')

class Model < ActiveRecord::Base
  before_create :generate_id  
  
  def self.set_prefix(prefix)
      @prefix = prefix
	  @primary_key = "#{@prefix}_id"
	  set_primary_key(@primary_key )
  end

  private 
  def self.model_primary_key
    @primary_key
  end
  
  def generate_id	
    cursor = ActiveRecord::Base.connection.execute("SELECT BMC_GEN_UNIQUE_KEY_FUNCTION('#{self.class.table_name}') AS ID FROM DUAL")	
	self[self.class.model_primary_key.to_sym] = cursor.fetch[0]
	cursor.close
  end  
end

class PersonRelationshipType < Model
end
 
class EditorialModel < Model
  set_table_name "BMC_EDITORIAL_MODEL"
  set_prefix "edm"
  
  has_many :person_relationship_types, :class_name => "PersonRelationshipType", :foreign_key=> 'pty_edm_id'
  
  def prts
	self.person_relationship_types
  end
end

class PersonRelationshipType < Model
  set_table_name "BMC_PERSON_RELATIONSHIP_TYPE"
  set_prefix "pty"
  
  belongs_to :editorial_model, :class_name => "EditorialModel", :foreign_key=> 'pty_edm_id'
end

class Manuscript < ActiveRecord::Base 
  set_table_name 'man'
end

EDM = EditorialModel
PTY = PersonRelationshipType
PRT = PersonRelationshipType
 
